package com.example.dell.wisdom_farm;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class view4 extends Fragment {

    private ListView listView1,listView2;

    private String[] title=new String[]{"空气温度值","土壤湿度值","CO₂浓度值","光照强度值"};

    private String[]  title1=new String[]{"用户密码设置"};
    private MyAdapter myAdapter1,myAdapter2;

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup,Bundle bundle)
    {

        View view=inflater.inflate( R.layout.view4,viewGroup, false);
        listView1=(ListView)view.findViewById(R.id.List_main_listview1);
         myAdapter1=new MyAdapter(title);
        listView1.setAdapter(myAdapter1);

        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog(position);
            }
        });
        listView2=(ListView)view.findViewById(R.id.List_main_listview2);
        myAdapter2=new MyAdapter(title1);
        listView2.setAdapter(myAdapter2);

        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog2(position);
            }
        });

        return  view;
    }

    public class MyAdapter extends BaseAdapter{
        private String[] titles;
        public  MyAdapter(String[] title)
        {
            this.titles=title;
        }

        @Override

        public int getCount(){return this.titles.length;}

        @Override
        public Object getItem(int position){
            return null;
        }

        @Override
        public  long getItemId(int position){
            return 0;
        }
        @Override
        public  View getView(int position,View convertView,ViewGroup parent)
        {
            View view =getLayoutInflater().from(getActivity()).inflate(R.layout.view4_item, null);

            TextView textView=view.findViewById(R.id.List_main_item_title);
            textView.setText(titles[position]);
            return  view;
        }

    }

    private  void dialog2(int position){
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setTitle(title1[position]);

        String str1="旧密码";
        String str2="新密码";
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.dialog2, null);

        final EditText editText1=(EditText)view.findViewById(R.id.dialog_edittext1);
        final EditText editText2=(EditText)view.findViewById(R.id.dialog_edittext2);

        TextView textView1=(TextView)view.findViewById(R.id.dialog_textview_title1);
        TextView textView2=(TextView)view.findViewById(R.id.dialog_textview_title2);
        textView1.setText(str1);
        textView2.setText(str2);

        builder.setView(view);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String tempStr=editText1.getText().toString();
            }
        });
        builder.setNegativeButton( "取消", null);
        builder.create().show();
    }
    private void dialog(int position){
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setTitle("报警值设置："+title[position]);
        String str1="最小值";
        String str2="最大值";
        int int1=1,int2=2;
        switch (position){
            case 0:int1=10;int2=40;
            str1+="(℃)";
            str2+="(℃)";
            break;
            case 1:
                int1=40;int2=100;
                str1+="(%)";
                str2+="(%)";
                break;
                case 2:
                    int1=100;int2 =2000;
                    str1+="(ppm)";
                    str2+="(ppm)";
                    break;
            case 3:int1=10;int2=50;
            str1+="(Klx)";
            str2+="(Klx)";
            break;

        }
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.dialog1, null);

        final EditText editText1=(EditText)view.findViewById(R.id.dialog_edittext1);
        final EditText editText2=(EditText)view.findViewById(R.id.dialog_edittext2);

        editText1.setText(""+int1);
        editText2.setText(""+int2);
        TextView textView1=(TextView)view.findViewById(R.id.dialog_textview_title1);
        TextView textView2=(TextView)view.findViewById(R.id.dialog_textview_title2);
        textView1.setText(str1);
        textView2.setText(str2);


        builder.setView(view);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String tempStr=editText1.getText().toString();
            }
        });
        builder.setNegativeButton( "取消", null);
        builder.create().show();
    }
}
